package njust.edu.controller;

import njust.edu.entity.*;
import njust.edu.exception.PasswordWrongException;
import njust.edu.service.*;
import njust.edu.service.impl.*;
import njust.edu.web.Page;
import njust.edu.web.SalaryOpe;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet(name = "UserController", value = "/UserController")
public class UserController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request, response);
    }

    // TODO Auto-generated catch block 获取密码
    public void getPassword(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String password = user.getPassword();
        PrintWriter out = response.getWriter();
        out.write(password);
        out.close();
    }

    // TODO Auto-generated catch block
    public void forPassword(HttpServletRequest request,
                            HttpServletResponse response) throws ServletException, IOException {
        String idStr = request.getParameter("id");
        Long id = 1l;
        try {
            id = Long.parseLong(idStr);
        } catch (NumberFormatException e) {
        }
        userService.editUserForPassword(id);

        PrintWriter out = response.getWriter();
        out.write("1");
        out.close();

    }

    // TODO Auto-generated catch block 管理员动用权限修改信息
    public void editUser(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String typeStr = request.getParameter("type");
        String tell = request.getParameter("tell");
        String pageNum = request.getParameter("pageNum");
        Integer type = 2;
        String idStr = request.getParameter("id");
        Long id = null;
        try {
            id = Long.parseLong(idStr);
        } catch (NumberFormatException e) {
        }
        try {
            type = Integer.parseInt(typeStr);
        } catch (NumberFormatException e) {
        }
        userService.editUser(id, name, type, tell);

        response.sendRedirect("userServlet?method=toUserManageUI&pageNum="
                + pageNum);

    }

    // TODO Auto-generated catch block 管理员动用权限新增一个员工信息
//    public void addUser(HttpServletRequest request, HttpServletResponse response)
//            throws ServletException, IOException {
//        String name = request.getParameter("name");
//        String typeStr = request.getParameter("type");
//        String tell = request.getParameter("tell");
//        Integer type = 2;
//        try {
//            type = Integer.parseInt(typeStr);
//        } catch (NumberFormatException e) {
//        }
//        userService.addUser(name, type, tell);
//
//        response.sendRedirect("userServlet?method=toUserManageUI");
//
//    }

    public void toModifyUI(HttpServletRequest request,
                           HttpServletResponse response) throws ServletException, IOException {
        String idStr = request.getParameter("id");
        Long id = null;
        try {
            id = Long.parseLong(idStr);
        } catch (NumberFormatException e) {
        }
        List<Role> roles = userService.getRoleList();
        User user = userService.getUserById(id);
        request.setAttribute("roles", roles);
        request.setAttribute("account", user);
        request.getRequestDispatcher("WEB-INF/jsp/user/user-add.jsp").forward(
                request, response);
    }

    public void userAddUI(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {

        List<Role> roles = userService.getRoleList();
        request.setAttribute("roles", roles);

        request.getRequestDispatcher("WEB-INF/jsp/user/user-add.jsp").forward(
                request, response);
    }


    // TODO Auto-generated catch block 删除用户信息
    public void delUser(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String pageStr = request.getParameter("pageNum");
        String idStr = request.getParameter("id");
        Long id = null;
        try {
            id = Long.parseLong(idStr);
        } catch (NumberFormatException e) {
        }
        userService.delUser(id);
        response.sendRedirect("userServlet?method=toUserManageUI&pageNum="
                + pageStr);

    }

    // TODO Auto-generated catch block 用户修改自己的密码
    public void editPassword(HttpServletRequest request,
                             HttpServletResponse response) throws ServletException, IOException,
            PasswordWrongException {
        HttpSession session = request.getSession();
        String oldPassword = request.getParameter("oldPassword");
        String password = request.getParameter("password");
        User user = (User) session.getAttribute("user");

        String sessionPassword = user.getPassword();
        if (oldPassword.equals(sessionPassword)) {
            User userNow = userService.edit(user.getId(), password);
            // user = userNow;
            session.setAttribute("user", userService.getUserById(user.getId()));
            request.setAttribute("message", " 修改成功");
        } else {
            request.setAttribute("message", "原来的密码错误");
        }

        request.getRequestDispatcher("WEB-INF/jsp/user/editPassword.jsp")
                .forward(request, response);

    }

    // TODO Auto-generated catch block 查看业绩
    public void toSalaryUI(HttpServletRequest request,
                           HttpServletResponse response) throws ServletException, IOException {
        String date = request.getParameter("date");
        String pageNoStr = request.getParameter("pageNo");

        //System.out.println(date);
        //System.out.println(pageNoStr);

        int pageNo = 1;
        try {
            pageNo = Integer.parseInt(pageNoStr);

        } catch (NumberFormatException e) {
        }

        HttpSession session = request.getSession();
        if (date != null) {
            session.setAttribute("salary_date", date);
        }

        date = (String) session.getAttribute("salary_date");
        //System.out.println(date);

        if (date != null) {
            session.setAttribute("salary_date_f", date.substring(0, 4) + "year"
                    + date.substring(5, 7) + "month");
            SalaryOpe salaryOpe = userService.getSalaryOpe(date, pageNo);
            //System.out.println(salaryOpe.getUserSalaryList());
            request.setAttribute("salaryOpe", salaryOpe);

        }
       //
        request.getRequestDispatcher("WEB-INF/jsp/user/salary.jsp").forward(
                request, response);
    }



    public void toFinanceUI(HttpServletRequest request,
                            HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/jsp/user/salaryAnalysis.jsp")
                .forward(request, response);
    }

    // TODO Auto-generated catch block 跳转到更改密码页面
    public void toUserPasswordEditUI(HttpServletRequest request,
                                     HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("WEB-INF/jsp/user/editPassword.jsp")
                .forward(request, response);
    }

    // TODO Auto-generated catch block 停止
    public void quit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.invalidate();
        response.sendRedirect("index.jsp");
    }
UserService userService = new UserServiceImpl();

    // TODO Auto-generated catch block 管理员跳转到账户管理页面
    public void toUserManageUI(HttpServletRequest request,
                               HttpServletResponse response) throws ServletException, IOException {
        String pageStr = request.getParameter("pageNum");
        int pageNum = 1;
        try {
            pageNum = Integer.parseInt(pageStr);
        } catch (NumberFormatException e) {
        }
        Page<User> page = userService.getPage(pageNum);
        request.setAttribute("page", page);
        request.getRequestDispatcher("WEB-INF/jsp/user/userManage.jsp")
                .forward(request, response);
    }

    // TODO Auto-generated catch block 跳转到个人信息页面
    public void toUserIndexUI(HttpServletRequest request,
                              HttpServletResponse response) throws ServletException, IOException {

        request.getRequestDispatcher("WEB-INF/jsp/user/userIndex.jsp").forward(
                request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String methodName = request.getParameter("method");
        try {
            Method method = getClass().getDeclaredMethod(methodName,
                    HttpServletRequest.class, HttpServletResponse.class);
            method.setAccessible(true);
            method.invoke(this, request, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    }

